package com.ai3d.rfid.ext.dialog

import android.text.TextUtils
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.WhichButton
import com.afollestad.materialdialogs.actions.getActionButton
import com.afollestad.materialdialogs.lifecycle.lifecycleOwner
import com.ai3d.rfid.common.utils.SettingUtil
import java.io.BufferedReader
import java.io.FileReader
import java.io.IOException

/**
 * @param message the content of the dialog ,required
 * @param title default title
 * @param positiveButtonText   the text of the positiveButton.default is empty
 * @param positiveAction  the action of the positiveButton default is empty action.
 * @param negativeButtonText   the text of the negativeButton.default is empty
 * @param negativeAction the action of the negativeButton default is empty action
 */
fun AppCompatActivity.showMessage(
    message: String,
    title: String = "温馨提示",
    positiveButtonText: String = "确定",
    positiveAction: () -> Unit = {},
    negativeButtonText: String = "",
    negativeAction: () -> Unit = {}
) {
    MaterialDialog(this)
        .cancelable(true)
        .lifecycleOwner(this)
        .show {
            title(text = title)
            message(text = message)
            positiveButton(text = positiveButtonText) {
                positiveAction.invoke()
            }
            if (negativeButtonText.isNotEmpty()) {
                negativeButton(text = negativeButtonText) {
                    negativeAction.invoke()
                }
            }
            getActionButton(WhichButton.POSITIVE).updateTextColor(SettingUtil.getColor(this@showMessage))
            getActionButton(WhichButton.NEGATIVE).updateTextColor(SettingUtil.getColor(this@showMessage))
        }
}

/**
 * @param message the content of the dialog ,required
 * @param title default title
 * @param positiveButtonText   the text of the positiveButton.default is empty
 * @param positiveAction  the action of the positiveButton default is empty action.
 * @param negativeButtonText   the text of the negativeButton.default is empty
 * @param negativeAction the action of the negativeButton default is empty action
 */
fun Fragment.showMessage(
    messageRes: Int,
    title: String = "温馨提示",
    positiveButtonText: String = "确定",
    positiveAction: () -> Unit = {},
    negativeButtonText: String = "",
    negativeAction: () -> Unit = {}
) {
    showMessage(
        activity?.getString(messageRes)!!,
        title,
        positiveButtonText,
        positiveAction,
        negativeButtonText,
        negativeAction
    )
}


/**
 * @param message the content of the dialog ,required
 * @param title default title
 * @param positiveButtonText   the text of the positiveButton.default is empty
 * @param positiveAction  the action of the positiveButton default is empty action.
 * @param negativeButtonText   the text of the negativeButton.default is empty
 * @param negativeAction the action of the negativeButton default is empty action
 */
fun Fragment.showMessage(
    message: String,
    title: String = "温馨提示",
    positiveButtonText: String = "确定",
    positiveAction: () -> Unit = {},
    negativeButtonText: String = "",
    negativeAction: () -> Unit = {}
) {
    activity?.let {
        MaterialDialog(it)
            .cancelable(true)
            .lifecycleOwner(viewLifecycleOwner)
            .show {
                title(text = title)
                message(text = message)
                positiveButton(text = positiveButtonText) {
                    positiveAction.invoke()
                }
                if (negativeButtonText.isNotEmpty()) {
                    negativeButton(text = negativeButtonText) {
                        negativeAction.invoke()
                    }
                }
                getActionButton(WhichButton.POSITIVE).updateTextColor(SettingUtil.getColor(it))
                getActionButton(WhichButton.NEGATIVE).updateTextColor(SettingUtil.getColor(it))
            }
    }
}

/**
 *retrieve process name by pid
 */
fun getProcessName(pid: Int): String? {
    var reader: BufferedReader? = null
    try {
        reader = BufferedReader(FileReader("/proc/$pid/cmdline"))
        var processName = reader.readLine()
        if (!TextUtils.isEmpty(processName)) {
            processName = processName.trim { it <= ' ' }
        }
        return processName
    } catch (throwable: Throwable) {
        throwable.printStackTrace()
    } finally {
        try {
            reader?.close()
        } catch (exception: IOException) {
            exception.printStackTrace()
        }

    }
    return null
}


fun List<*>?.isNull(): Boolean {
    return this?.isEmpty() ?: true
}

fun List<*>?.isNotNull(): Boolean {
    return this != null && this.isNotEmpty()
}


